package com.mtx.weatherreport;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.mtx.weatherreport.Application.MyApplication;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

public class SelectCityActivity extends AppCompatActivity {

    @BindView(R.id.listview_city)
    ListView mListViewCity;
    @BindView(R.id.edit_select_city)
    EditText mEditSelectCity;

    ArrayList<String> mCityList;  // 城市+省份名称
    ArrayList<String> mCityNumber;  // 只保存城市编号
    ArrayList<String> mCityListSelected;  // 城市+省份名称
    ArrayList<String> mCityNumberSelected;  // 只保存城市编号
    MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_city);

        ButterKnife.bind(this);

        myApplication = (MyApplication)this.getApplication();
        mCityList = myApplication.getListCity();  // 将数据库中的城市名称取出来
        mCityNumber = myApplication.getmCityNumber();  // 将数据库中的城市编号取出来
        mCityListSelected = myApplication.getListCity2();  // 没有的话，后边的city_list.clear()之后相当于mCityListSelected也会为空
        mCityNumberSelected = myApplication.getmCityNumber2();
        mCityList.clear();  // 清空原有值
        mCityNumber.clear();

        for(int i=0;i<mCityListSelected.size();i++) {  //重新赋值
            mCityList.add(i,mCityListSelected.get(i));
            mCityNumber.add(i,mCityNumberSelected.get(i));
        }

        mListViewCity = (ListView)findViewById(R.id.listview_city);
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item,mCityList);
        mListViewCity.setAdapter(adapter);

        /**
         * 输入搜索内容
         */
        mEditSelectCity.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                final String cityName = mEditSelectCity.getText().toString();
                int t = 0;
                mCityList.clear();
                mCityNumber.clear();

                for(int k=0;k<mCityListSelected.size();k++) {
                    if(mCityListSelected.get(k).indexOf(cityName) != -1) // 输入的内容是城市名称的一部分
                    {
                        mCityList.add(t,mCityListSelected.get(k));
                        mCityNumber.add(t,mCityNumberSelected.get(k));
                        t++;
                    }
                }
                adapter.notifyDataSetChanged();  // 更新适配器
            }
        });
    }

    /**
     * 点击listview中的内容
     * @param position
     */
    @OnItemClick(R.id.listview_city)
    void onItemClick(int position){
        //将数据保存到SharedPreferences中
        Context context=this;
        SharedPreferences sharedPreferences = context.getSharedPreferences("weather_info",MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("number",mCityNumber.get(position));
        editor.commit();

        setResult(0,getIntent());  // 与MainActivity传参数
        finish();
    }

    /**
     * 点击返回按钮
     */
    @OnClick(R.id.return_city_img)
    void onReturn() {
        finish();
    }
}
